<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8" trimDirectiveWhitespaces="true"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ include file="/WEB-INF/jsp/common/taglib.jsp"%>
<!DOCTYPE html>
<html>
<head>
<title>添加角色</title>
<jsp:include page="../addlink.jsp" />
<link rel="stylesheet" type="text/css"
	href="${pageContext.request.contextPath}/statics/css/public_form.css">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/statics/css/system_add.css">
<link rel="stylesheet"
	href="${pageContext.request.contextPath}/statics/lib/zTree/css/zTreeStyle/zTreeStyle.css"
	type="text/css">
<script type="text/javascript"
	src="${pageContext.request.contextPath}/statics/lib/zTree/js/jquery-1.4.4.min.js"></script>
<script type="text/javascript"
	src="${pageContext.request.contextPath}/statics/lib/zTree/js/jquery.ztree.core.js"></script>
<script type="text/javascript"
	src="${pageContext.request.contextPath}/statics/lib/zTree/js/jquery.ztree.excheck.js"></script>
</head>
<body>
	<jsp:include page="../head.jsp" />

	<div class="container-fluid all">
		<jsp:include page="../menu.jsp" />
		<div class="maincontent">
			<ol class="breadcrumb">
				<li class="url-root"><a>系统设置</a></li>
				<li class="url-root"><a>权限管理</a></li>
				<li class="active">添加角色</li>
			</ol>
			<div class="mode form_mode">
				<form class="form-horizontal" method="post" action="#">
					<input type="hidden" name="id" value="${role.id}"/>
					<div class="form-group">
						<label class="col-sm-2 control-label">角色名称</label>
						<div class="col-sm-10">
							<input type="text" class="form-control" name="name" value="${role.name}">
						</div>
					</div>
					<div class="form-group">
						<label class="col-sm-2 control-label">角色描述</label>
						<div class="col-sm-10">
							<input type="text" class="form-control" name="desc" value="${role.desc}">
						</div>
					</div>
					<div class="form-group">
						<label class="col-sm-2 control-label">角色状态</label>
						<div class="col-sm-10">
							<select class="form-control" name="avaliable">
								<option value="1">启用</option>
								<option value="0">禁用</option>
							</select>
						</div>
					</div>
					<div class="form-group">
						<label class="col-sm-2 control-label"></label>
						<div class="col-sm-5">
						<div class="input-title">选择菜单</div>
							<div class="zTreeDemoBackground left">
								<ul id="treeMenu" class="ztree"></ul>
							</div>
						</div>
						<div class="col-sm-5">
						<div class="input-title">选择部门</div>
							<div class="zTreeDemoBackground left">
								<ul id="treeDept" class="ztree"></ul>
							</div>
						</div>
					</div>
					<div class="form-group">
						<div class="col-sm-12">
							<span class="btn btn-success" id="saveRole">保存</span>

						</div>
					</div>
				</form>
			</div>
		</div>
	</div>

<jsp:include page="../foot.jsp" />

</body>
<SCRIPT type="text/javascript">

$(function(){
	var editFlag = ${editFlag};
	url = "${basePath}/sys/role/saveRole";
	if(editFlag == 2){
		url = "${basePath}/sys/role/updateRole";
	}
	$("#saveRole").click(function(){
		//获取字典修改后的信息数据,组装成json字符串
		var formObject = {};
		var formArray =$("form:first").serializeArray();
		$.each(formArray, function(i, item){
				formObject[item.name]=item.value;
		});
		var roleVo = {};
		roleVo['role'] = formObject;
		//获取权限的值
	 	var roleCheckObj ={};
		  var treeObj = $.fn.zTree.getZTreeObj("treeMenu");
		  var nodes = treeObj.getCheckedNodes(true);
		  var str="";
		  for(var i=0;i<nodes.length;i++){
			  roleCheckObj[nodes[i].id]=nodes[i].id;
		               }
		roleVo["menuIds"]=roleCheckObj;
		//获取部门的值
	 	var roleCheckObj ={};
		  var treeObj = $.fn.zTree.getZTreeObj("treeDept");
		  var nodes = treeObj.getCheckedNodes(true);
		  var str="";
		  for(var i=0;i<nodes.length;i++){
			  roleCheckObj[nodes[i].keyId]=nodes[i].keyId;
		               }
		roleVo["deptIds"]=roleCheckObj;
		
		console.log(JSON.stringify(roleVo));
		//return false;
 		$.ajax({
			type:"POST",
			url:url,
			dataType:'json', 
			contentType :"application/json;charset=UTF-8",
			data: JSON.stringify(roleVo),//传递给服务器的参数	
			success:function(data){
				if(checkTokenError(data)){
				if(data.code == -998){
					alert(data.message);
				}else if(data.code == 200){
					setTimeout(function(){
						//history.go(-1);
						window.location.href = "${basePath}/sys/role/toRoleList";
					},1000);
					zxTips_short({txt:data.message});
				}
				//console.log(data.message);
				}
			},
			error:function(req,res){
				console.log(req);
				console.log(res);
			}
		}); 
	});
});

	var setting = {
		check : {
			enable : true
		},
		data : {
			simpleData : {
				enable : true
			}
		}
	};


 var zMenuData = [ 
	<c:forEach items="${treeMenuList}" var="menu">
	{
		id: "${menu.id}",
		pId: "${menu.parentId}", 
		name: "${menu.name}",
		open:true
	}, 
</c:forEach>         
   ];
 var zDeptData = [ 
	<c:forEach items="${treeDeptList}" var="dept">
	{
		id: "${dept.branCode}",
		pId: "${dept.admBranCode}", 
		name: "${dept.branName}",
		keyId:"${dept.id}",
		open:true
	}, 
</c:forEach>         
   ];

 
 
 var menuTree = $.fn.zTree.init($("#treeMenu"), setting, zMenuData);
 var deptTree = $.fn.zTree.init($("#treeDept"), setting, zDeptData);
//进入修改页面的时候 ，选中已经拥有资源的节点	
	<c:forEach items="${roleMenus}" var="roleMenu">
		var node = menuTree.getNodeByParam("id","${roleMenu.menuId}");
		menuTree.checkNode(node,true,false);
	</c:forEach>
	<c:forEach items="${roleDepts}" var="roleDept">
		var node = deptTree.getNodeByParam("keyId","${roleDept.deptId}");
		deptTree.checkNode(node,true,false);
	</c:forEach>

	

	
</SCRIPT>




</html>
